﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using nwrole.Supervisor.web.Models;
using nwrole.Supervisor.web.Utils;
using nwrole.lib;

namespace nwrole.Supervisor.web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            HomeIndexViewModel viewModel = new HomeIndexViewModel();

            using (var supervisor = GetSupervisorChannel())
            {
                viewModel.SupervisorData = supervisor.Channel.GetSupervisorData();
                
                ServiceData[] serviceDatas = supervisor.Channel.GetServices();
                foreach(ServiceData serviceData in serviceDatas)
                {
                    HomeIndexViewModel.ServiceViewModel serviceViewModel = new HomeIndexViewModel.ServiceViewModel();
                    serviceViewModel.ServiceData = serviceData;

                    ServicePulse servicePulse = supervisor.Channel.GetServicePulse(serviceData.Id);
                    if(servicePulse != null)
                    {
                        serviceViewModel.LastPulseDate = servicePulse.Date.ToLocalTime();
                        serviceViewModel.LastPulseMessage = servicePulse.Message;
                    }

                    serviceViewModel.Status = HomeIndexViewModel.Status.Disabled;
                    if(serviceData.Enabled)
                    {
                        if(serviceViewModel.LastPulseDate.HasValue && serviceViewModel.LastPulseDate.Value > DateTime.Now.AddMinutes(-5))
                            serviceViewModel.Status = HomeIndexViewModel.Status.Ok;
                        else
                            serviceViewModel.Status = HomeIndexViewModel.Status.Error;
                    }

                    PluginData[] pluginDatas = supervisor.Channel.GetServicePluginsData(serviceData.Id);
                    foreach(PluginData pluginData in pluginDatas)
                    {
                        HomeIndexViewModel.PluginViewModel pluginViewModel = new HomeIndexViewModel.PluginViewModel();
                        pluginViewModel.PluginData = pluginData;

                        PluginPulse pluginPulse = supervisor.Channel.GetPluginPulse(pluginData.Id);
                        if(pluginPulse != null)
                        {
                            pluginViewModel.LastPulseDate = pluginPulse.Date.ToLocalTime();
                            pluginViewModel.LastPulseMessage = pluginPulse.Message;
                        }

                        pluginViewModel.Status = HomeIndexViewModel.Status.Disabled;
                        if (pluginData.Enabled)
                        {
                            if (pluginViewModel.LastPulseDate.HasValue && pluginViewModel.LastPulseDate.Value > DateTime.Now.AddMinutes(-5))
                                pluginViewModel.Status = HomeIndexViewModel.Status.Ok;
                            else
                                pluginViewModel.Status = HomeIndexViewModel.Status.Error;
                        }

                        serviceViewModel.Plugins.Add(pluginViewModel);
                    }

                    viewModel.Services.Add(serviceViewModel);
                }
            }

            return View(viewModel);
        }

        public ActionResult RestartService(Guid id)
        {
            using (var supervisor = GetSupervisorChannel())
            {
                supervisor.Channel.RestartService(id);
            }

            return RedirectToAction("Index");
        }

        public ActionResult StopService(Guid id)
        {
            using (var supervisor = GetSupervisorChannel())
            {
                supervisor.Channel.StopService(id);
            }

            return RedirectToAction("Index");
        }

        public ActionResult StartService(Guid id)
        {
            using (var supervisor = GetSupervisorChannel())
            {
                supervisor.Channel.StartService(id);
            }

            return RedirectToAction("Index");
        }

        /*public ActionResult StopPlugin(Guid id)
        {
            using (var supervisor = GetSupervisorChannel())
            {
                supervisor.Channel
            }

            return RedirectToAction("Index");
        }*/

        private TcpChannel<ISupervisorContract> GetSupervisorChannel()
        {
            var supervisor = new TcpChannel<ISupervisorContract>(SiteSettings.SupervisorAddress);
            return supervisor;
        }
    }
}
